﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Renren.Helpers;
using Renren.Models;
using Renren.ViewModels;

namespace Renren.Services
{
    public static class UserService
    {
        public static void GetLoginUserInfo(UserModel user)
        {
            if (user.UserId == 0 && RenrenSettings.CacheEnabled)
            {
                Service.FetchStorage(RenrenSettings.LoginUserKey, str =>
                    {
                        var root = XElement.Parse(str);
                        var ns = root.GetDefaultNamespace();
                        foreach (var element in root.Elements())
                        {
                            ParseUserXml(user, element);
                        }
                    });
            }
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "users.getInfo");
            parameters.Add("fields", "uid,name,sex,star,zidou,vip,birthday,email_hash,tinyurl,headurl,mainurl,hometown_location,work_history,university_history");
            Service.SendRequest(
                parameters,
                result =>
                {
                    foreach (var element in XElement.Parse(result).Elements())
                    {
                        ParseUserXml(user, element);
                    }
                    Service.WritebackStorage(RenrenSettings.LoginUserKey, result);
                });
        }

        private static void GetUserProfilePhoto(UserViewModel userView, long id)
        {
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "users.getInfo");
            parameters.Add("uids", id.ToString());
            parameters.Add("fields", "mainurl");
            Service.SendRequest(
                parameters,
                result =>
                {
                    var element = XElement.Parse(result).Child("user");
                    userView.User.MainUrl = element.GetValue("mainurl");
                    userView.User = userView.User;
                });
        }

        public static void GetUserProfile(UserViewModel userView, long id)
        {
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "users.getProfileInfo");
            parameters.Add("uid", id.ToString());
            parameters.Add("fields", "base_info,status,visitor_count,friends_count");
            Service.SendRequest(
                parameters,
                result =>
                {
                    var user = new UserModel();
                    var element = XElement.Parse(result);
                    user.UserId = element.GetValue<long>("uid");
                    user.Name = element.GetValue("name");
                    user.HeadUrl = element.GetValue("headurl");
                    user.Star = element.GetValue<int>("star");
                    user.NetworkName = element.GetValue("network_name");
                    user.Gender = element.GetValue<int>("base_info", "gender");

                    // Parse birthday
                    user.BirthYear = element.GetValue("base_info", "birth", "birth_year");
                    user.BirthMonth= element.GetValue("base_info", "birth", "birth_month");
                    user.BirthDay= element.GetValue("base_info", "birth", "birth_day");

                    user.Province = element.GetValue("base_info", "hometown", "province");
                    user.City = element.GetValue("base_info", "hometown", "city");
                    user.StatusContent = element.GetValue("status", "content");
                    user.StatusTime = element.GetValue<DateTime>("status", "time");
                    user.VisitorsCount = element.GetValue<long>("visitors_count");
                    user.FriendsCount = element.GetValue<long>("friends_count");

                    userView.User = user;
                    GetUserProfilePhoto(userView, id);
                });
        }

        private static void ParseUserXml(UserModel user, XElement element)
        {
            user.UserId = element.GetValue<long>("uid");
            user.Name = element.GetValue("name");
            user.HeadUrl = element.GetValue("headurl");
            user.TinyUrl = element.GetValue("tinyurl");
        }
    }
}
